package com.gotloop.jdo.key;

import java.io.Serializable;
import java.util.StringTokenizer;

import javax.jdo.identity.ObjectIdentity;

import com.gotloop.jdo.UserProfile;

/**
 * Primary Key of Following.
 * @author jibhaine
 *
 */
public class FollowPK implements Serializable {
	
	/**
	 * Generated serial version UID.
	 */
	private static final long serialVersionUID = -4546681008341062358L;

	/**
	 * UserA primary key (following UserB).
	 */
	public ObjectIdentity follower;

	/**
	 * UserB PK (followed by UserA).
	 */
	public ObjectIdentity followed;

	/**
	 * Public constructor.
	 */
	public FollowPK() {
		super();
	}

	/**
	 * Constructor with fields.
	 * @param followStrPK	String of the form "follower.name::followed.name"
	 */
	public FollowPK(String followStrPK) {
		super();
		StringTokenizer token = new StringTokenizer(followStrPK, "::");
        this.follower = new ObjectIdentity(UserProfile.class, token.nextToken());
        this.followed = new ObjectIdentity(UserProfile.class, token.nextToken());
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		if (followed == null) {
			result = prime * result	+ 0;
		} else {
			result = prime * result	+ followed.hashCode();
		}
		if (follower == null) {
			result = prime * result	+ 0;
		} else {
			result = prime * result	+ follower.hashCode();
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		boolean ret = false;
		if (obj != null && (obj instanceof FollowPK)) {
			FollowPK otherPK = (FollowPK) obj;
			ret = otherPK.follower.equals(this.follower) 
				&& otherPK.followed.equals(this.followed);
		}
		return ret;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return follower.toString() + "::" + followed.toString();
	}
	
}
